﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MySql.Data.MySqlClient;

namespace Fotball_01.DB
{
    class Database
    {
        private MySqlConnection connection;
        private MySqlCommand command;
        private MySqlDataReader reader;

        public Database()
        {
            string connectQuery = "SERVER=localhost;" +
                "DATABASE=fotball;" +
                "UID=root;" +
                "PASSWORD=omgplz;";

            connection = new MySqlConnection(connectQuery);
            command = connection.CreateCommand();
            connection.Open();
        }

        /// <summary>
        /// Closes the database connection
        /// </summary>
        ~Database()
        {
            connection.Close();
        }

        public List<List<string>> Query(string query)
        {
            command.CommandText = query;
            reader = command.ExecuteReader();

            List<List<string>> result = new List<List<string>>();

            int count = 0;
            while (reader.Read())
            {                
                // Ny rad
                result.Add(new List<string>());

                for (int i = 0; i < reader.FieldCount; i++)
                {
                    // Ny kolonne
                    result[count].Add(reader.GetValue(i).ToString());
                }

                // Legg til telleren
                count++;
            }

            // Lukk leseren for å unngå exception
            reader.Close();

            return result;
        }
    }
}
